package thread;

import java.io.ObjectOutputStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class testDemo27 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolExecutor myThreadPoolExecutor=new MyThreadPoolExecutor(4);
        for (int i = 1; i <100 ; i++) {
            int n=i;
            myThreadPoolExecutor.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("这是线程"+Thread.currentThread().getName()+"执行第"+n+"个任务");
                }
            });
        }
    }
}
class MyThreadPoolExecutor{
    BlockingQueue<Runnable> queue=new ArrayBlockingQueue(100);
    public MyThreadPoolExecutor(int n){
        for (int i = 0; i <n ; i++) {
            Thread t=new Thread(()->{
                while (true){
                    try {
                        Runnable runnable=queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}
